package com.lb.shopguide.ui.fragment.guide.pay;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.order.CommitOrderReplyBean;
import com.lb.shopguide.event.guide.GiveupPayEvent;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.ui.dialog.DialogChangePayType;
import com.lb.shopguide.ui.dialog.DialogMakeSure;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.lb.shopguide.util.lb.BizUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * Created by kang on 2017/6/9.
 */

public class FragmentPayCash extends BaseCommonFragment {

    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_change_type)
    TextView tvPayType;
    @BindView(R.id.tv_pay)
    TextView tvPay;

    private CommitOrderReplyBean mReplyBean;
    private String mMemberCode;

    public static FragmentPayCash newInstance(CommitOrderReplyBean replyBean, String memberCode) {
        FragmentPayCash fragmentPayCash = new FragmentPayCash();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConstant.COMMIT_REPLY_BEAN, replyBean);
        bundle.putString(AppConstant.MEMBER_CODE, memberCode);
        fragmentPayCash.setArguments(bundle);
        return fragmentPayCash;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_pay_cash;
    }

    @Override
    public void initData() {
        mUserConfigManager = UserConfigManager.getInstance(mContext);
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.COMMIT_REPLY_BEAN)) {
            mReplyBean = (CommitOrderReplyBean) bundle.getSerializable(AppConstant.COMMIT_REPLY_BEAN);
        }
        if (bundle.containsKey(AppConstant.MEMBER_CODE)) {
            mMemberCode = bundle.getString(AppConstant.MEMBER_CODE);
        }
    }

    @Override
    public void initView() {
        ntb.setTitleText("现金收款");
        ntb.setTitleColor(getResources().getColor(R.color.white));
        ntb.setBackground(getResources().getColor(R.color.sg_common_red));
        ntb.setLeftImageSrc(R.drawable.iv_back_white);
        ntb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBackDialog();
            }
        });

        bindData();
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {
        ((GuideMainActivity) getActivity()).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
    }

    @Override
    public String getPageTag() {
        return FragmentPayCash.class.getName();
    }

    @Override
    public boolean onBackPressedSupport() {
        showBackDialog();
        return true;
    }

    private void showBackDialog() {
        DialogMakeSure dialog = new DialogMakeSure(mContext);
        dialog.getDialog("是否放弃本次支付?").show();
        dialog.setOnDialogClickListener(new DialogMakeSure.OnDialogClickListener() {
            @Override
            public void onSure(DialogInterface dialog, int which) {
                EventBus.getDefault().post(new GiveupPayEvent());
                sendRequestCloseOrder();
            }

            @Override
            public void onCancel(DialogInterface dialog, int which) {
            }
        });
    }

    private void bindData() {
        tvPrice.setText("￥" + mReplyBean.getOrderTotalMoney());
    }

    private void sendRequestCloseOrder() {
        HashMap hashMap = new HashMap();
        hashMap.put("orderNum", mReplyBean.getOrderNum());
        RequestBody body = BizUtil.getBodyFromMap(hashMap);
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == (ResponseCode.RESPONSE_SUCCESS)) {
                } else {
                    ToastUtils.showShort(response.getMessage());
                }
                getActivity().finish();
            }
        };
        ApiMethodGuide.closeOrder(new SimpleLbObserver(listener, null), body, this, FragmentEvent.DESTROY_VIEW);
    }

    private void sendRequestPayCashConfirm() {
        HashMap hashMap = new HashMap();
        hashMap.put("orderNum", mReplyBean.getOrderNum());
        hashMap.put("staffCode", mUserConfigManager.getUserId());
        RequestBody body = BizUtil.getBodyFromMap(hashMap);
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == (ResponseCode.RESPONSE_SUCCESS)) {
                    //                        EventBus.getDefault().post(new PaySuccessEvent());
                    ((FragmentPayOrder) getParentFragment()).start(FragmentPayResult.newInstance(true, mReplyBean));
                } else {
                    ToastUtils.showShort(response.getMessage());
                    ((FragmentPayOrder) getParentFragment()).start(FragmentPayResult.newInstance(false, mReplyBean));
                }
            }
        };
        ApiMethodGuide.payCashConfirm(new SimpleLbObserver(listener, null), body, this, FragmentEvent.DESTROY_VIEW);
    }

    @OnClick(R.id.tv_change_type)
    void changeType() {
        DialogChangePayType dialogChangePayType = DialogChangePayType.getDialogChangePayType(mReplyBean, mMemberCode);
        dialogChangePayType.show(getFragmentManager(), getPageTag());
    }

    @OnClick(R.id.tv_pay)
    void surePay() {
        DialogMakeSure dialog = new DialogMakeSure(mContext);
        dialog.getDialog("确定支付完成吗?").show();
        dialog.setOnDialogClickListener(new DialogMakeSure.OnDialogClickListener() {
            @Override
            public void onSure(DialogInterface dialog, int which) {
                sendRequestPayCashConfirm();
            }

            @Override
            public void onCancel(DialogInterface dialog, int which) {
            }
        });
    }

}
